home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / lib / flyEngine / flyMultiplayer.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-12  |  4.7 KB  |  189 lines

  1. #include "../Fly3D.h"
  2. #include "flyMultiplayer.h"
  3.  
  4. FLY_API GUID
  5. FLYGUID = { /* e29ac960-09f8-11d2-9262-000021799871 */
  6.     0xe29ac960,
  7.     0x09f8,
  8.     0x11d2,
  9.     {0x92, 0x62, 0x00, 0x00, 0x21, 0x79, 0x98, 0x71}
  10.   };
  11.  
  12. FLY_API BOOL FAR PASCAL MultiplayerProc(HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
  13. {
  14.     static mp_games *g=0;
  15.     switch (uiMsg)
  16.     {
  17.     case WM_COMMAND:
  18.         switch(LOWORD(wParam))
  19.         {
  20.         case IDC_SEARCH:
  21.             {
  22.             char ip[256];
  23.             GetDlgItemText(hWnd,IDC_IP,ip,255);
  24.             SendDlgItemMessage(hWnd,IDC_GAMES,LB_RESETCONTENT,0,0);
  25.             directx->init_multiplayer(ip);
  26.             g=directx->enum_games(&FLYGUID);
  27.             if (g)
  28.                 {
  29.                 int i,j;
  30.                 char str[256];
  31.                 for( i=0;i<g->num;i++ )
  32.                     {
  33.                     sprintf(str,"%s (%i)",g->name[i],g->num_players[i]);
  34.                     j=SendDlgItemMessage(hWnd,IDC_GAMES,LB_ADDSTRING,0,(DWORD)str);
  35.                     SendDlgItemMessage(hWnd,IDC_GAMES,LB_SETITEMDATA,j,(DWORD)i);
  36.                     }
  37.                 if (g->num)
  38.                     {
  39.                     SendDlgItemMessage(hWnd,IDC_GAMES,LB_SETCURSEL,0,0);
  40.                     EnableWindow(GetDlgItem(hWnd,IDOK),1);
  41.                     SetFocus(GetDlgItem(hWnd,IDOK));
  42.                     }
  43.                 else EnableWindow(GetDlgItem(hWnd,IDOK),0);
  44.                 }
  45.             }
  46.             break;
  47.         case IDOK:
  48.             {
  49.             char playername[64];
  50.             GetDlgItemText(hWnd,IDC_PLAYERNAME,playername,64);
  51.             if (strlen(playername)==0)
  52.                 {
  53.                 MessageBox(hWnd,"Please enter a player name.","Fly3D",MB_OK);
  54.                 break;
  55.                 }
  56.             int j=SendDlgItemMessage(hWnd,IDC_GAMES,LB_GETCURSEL,0,0);
  57.             if (j==-1)
  58.                 {
  59.                 MessageBox(hWnd,"Please select a game to join.","Fly3D",MB_OK);
  60.                 break;
  61.                 }
  62.             j=SendDlgItemMessage(hWnd,IDC_GAMES,LB_GETITEMDATA,j,0);
  63.             j=directx->join_game(&g->guid[j],playername);
  64.             EndDialog(hWnd,IDOK);
  65.             }
  66.             break;
  67.         case IDCANCEL:
  68.             directx->free_multiplayer();
  69.             EndDialog(hWnd,IDCANCEL);
  70.             break;
  71.         }
  72.         break;
  73.  
  74.     case WM_INITDIALOG:
  75.         SetFocus(GetDlgItem(hWnd,IDC_PLAYERNAME));
  76.         EnableWindow(GetDlgItem(hWnd,IDOK),0);
  77.         directx->init_multiplayer();
  78.         break;
  79.     }
  80.     return FALSE;
  81.  
  82. void flyEngine::close_multiplayer()
  83. {
  84.     int i;
  85.     for( i=1;i<directx->nplayers;i++ )
  86.     {
  87.         bsp_object *obj=(bsp_object *)directx->players[i].data;
  88.         obj->life=-1;
  89.     }
  90.     directx->free_multiplayer();
  91. }
  92.  
  93. int flyEngine::join_multiplayer()
  94. {
  95.     close_multiplayer();
  96.     close_fly_file();
  97.     if (IDOK==DialogBox(hFlyInst,MAKEINTRESOURCE(IDD_MULTIPLAYER),hFlyWnd,(DLGPROC)MultiplayerProc))
  98.     {
  99.         int maxtime=5000;
  100.  
  101.         while(maxtime>0)
  102.         {
  103.         maxtime-=500;
  104.         Sleep(500);
  105.         check_multiplayer();
  106.         if (flyfile[0]!=0) break;
  107.         }
  108.         if (flyfile[0]==0)
  109.             close_multiplayer();
  110.         directx->players[0].data=player;
  111.     }
  112.     return 0;
  113. }
  114.  
  115. void flyEngine::check_multiplayer()
  116. {
  117.     int i,j,num,from;
  118.     mp_msg *msg;
  119.     DWORD size;
  120.  
  121.     num=directx->get_num_messages();
  122.     for( i=0;i<num;i++ )
  123.     {
  124.         size=500;
  125.         msg=directx->get_message(&size);
  126.         if (msg)
  127.         {
  128.         for( from=0;from<directx->nplayers;from++ )
  129.             if (directx->players[from].dpid==msg->from)
  130.                 break;
  131.         if (from==directx->nplayers)
  132.             from=-1;
  133.         switch(msg->type)
  134.             {
  135.             case DPSYS_CREATEPLAYERORGROUP:
  136.                 {
  137.                     DPMSG_CREATEPLAYERORGROUP *info=(DPMSG_CREATEPLAYERORGROUP *)&msg->type;
  138.  
  139.                     mp_msg msgflyfile;
  140.                     strcpy(msgflyfile.data,flyfile);
  141.                     msgflyfile.type=FLYMP_MSG_FLYFILE;
  142.                     msgflyfile.from=DPID_SERVERPLAYER;
  143.                     directx->send_message(&msgflyfile,strlen(flyfilename)+9,info->dpId);
  144.                     
  145.                     player_data pd;
  146.                     pd.type=FLYMP_MSG_JOIN;
  147.                     pd.dpid=info->dpId;
  148.                     strcpy(pd.name,info->dpnName.lpszShortNameA);
  149.                     dll.send_message(FLYM_MPMESSAGE,-1,&pd);
  150.  
  151.                     directx->players[directx->nplayers-1].type=FLYMP_MSG_JOIN;
  152.                     for( i=1;i<directx->nplayers-1;i++ )
  153.                         {
  154.                         directx->players[i].type=FLYMP_MSG_JOIN;
  155.                         directx->send_message((mp_msg *)&directx->players[i],FLYMP_PLAYERDATASIZE,info->dpId);
  156.                         directx->send_message((mp_msg *)&directx->players[directx->nplayers-1],FLYMP_PLAYERDATASIZE,directx->players[i].dpid);
  157.                         }
  158.                 }
  159.                 break;
  160.             case DPSYS_DESTROYPLAYERORGROUP:
  161.                 {
  162.                     DPMSG_DESTROYPLAYERORGROUP *info=(DPMSG_DESTROYPLAYERORGROUP *)&msg->type;
  163.                     for( j=0;j<directx->nplayers;j++ )
  164.                         if (directx->players[j].dpid==info->dpId)
  165.                             break;
  166.                     if (j<directx->nplayers)
  167.                         {
  168.                         directx->players[j].type=FLYMP_MSG_QUIT;
  169.                         directx->send_message((mp_msg *)&directx->players[j],FLYMP_PLAYERDATASIZE);
  170.                         dll.send_message(FLYM_MPMESSAGE,j,&directx->players[j]);
  171.                         }
  172.                 }
  173.                 break;
  174.             case DPSYS_SESSIONLOST:
  175.                 close_multiplayer();
  176.                 return;
  177.             case FLYMP_MSG_FLYFILE:
  178.                 open_fly_file(msg->data);
  179.                 break;
  180.             default:
  181.                 dll.send_message(FLYM_MPMESSAGE,from,msg);
  182.                 if (directx->mpmode==FLYMP_SERVER)
  183.                     directx->send_message(msg,size);
  184.             }
  185.         }
  186.     }
  187. }
  188.